qui(function (require, exports, module) {
    Record.Signal_Util = $.extend(true, {}, EpgWeb.Util, {
        formatter: {
            operator_signal_list: function (v, o, r) {
                var htmlArr = [];
                htmlArr.push("<a href='javascript:;' onclick='Record.Signal_Util.openSignalModifyDlg(" + r.id + ")' style='margin-right: 10px'>修改</a>");
                htmlArr.push("<a href='javascript:;' onclick='Record.Signal_Util.delSignal(" + r.id + ")' >删除</a>");
                return htmlArr.join("");
            },
            operator_channel_signal_list: function (v, o, r) {
                var htmlArr = [];
                // console.error(JSON.stringify(r))
                if (r.status === 1) {
                    htmlArr.push("<a href='javascript:;' onclick='Record.Signal_Util.stopChannelSignal(" + r.id + ")' style='margin-right: 10px'>关闭</a>");
                }else{
                    htmlArr.push("<a href='javascript:;' onclick='Record.Signal_Util.startChannelSignal(" + r.id + ")' style='margin-right: 10px'>启动</a>");
                }
                // htmlArr.push("<a href='javascript:;' onclick='Record.Signal_Util.openChannelSignalModifyDlg(" + r.id + ")' style='margin-right: 10px'>修改</a>");
                htmlArr.push("<a href='javascript:;' onclick='Record.Signal_Util.delChannelSignal(" + r.id + ")' >删除</a>");
                return htmlArr.join("");
            },
            status: function (v, o, r) {
                return v === 1 ? "上线" : "下线";
            },

            status_channel_signal: function (v, o, r) {
                return v === 1 ? "启动" : "关闭";
            },
            priority: function (v, o, r) {
                var result_priority = "";
                switch (v) {
                    case 1 :
                        result_priority = "低";
                        break;
                    case 2 :
                        result_priority = "较低";
                        break;
                    case 3 :
                        result_priority = "中";
                        break;
                    case 4 :
                        result_priority = "高";
                        break;

                    default:
                        break;
                }

                return result_priority;

            }

        },
        reloadHandler: function (data) {
            if (data != null) {
                EpgWeb.Plugin.showTips({content: data.msg, type: data.code == "A00000" ? "success" : "error"});
                if (data.code == "A00000") {
                    $("#list").trigger("reloadGrid")
                }
            } else {
                console.log("callbackHandler no return data");
            }
        },

        prepareSignalMode: function () {
            var sendData = {};
            var parentDom = $("form[role=addForm]");
            var signalId = parentDom.find("input[name=signal_id]").val();
            if(signalId.length !== 0 ){
                sendData.id = signalId;
            }
            sendData.signalName = parentDom.find("input[name=signal_name]").val().trim();
            sendData.signalAddress = parentDom.find("input[name=signal_address_name]").val().trim();
            sendData.serverCluster = parentDom.find("input[name=server_cluster_name]").val().trim();
            sendData.priority = parseInt(parentDom.find("select[name=priority_name]").val());
            sendData.status = parseInt(parentDom.find("select[name=status]").val());
            if ($.trim(sendData.signalName).length === 0) {
                EpgWeb.Plugin.showTips({ theme: "error", content: "请填写信号名称"});
                return null;
            }
            if ($.trim(sendData.signalAddress).length === 0) {
                EpgWeb.Plugin.showTips({ theme: "error", content: "请填写信号流地址"});
                return null;
            }

            if ($.trim(sendData.serverCluster).length === 0) {
                EpgWeb.Plugin.showTips({ theme: "error", content: "请选择服务器集群"});
                return null;
            }

            if (sendData.priority  === 0) {
                EpgWeb.Plugin.showTips({ theme: "error", content: "请选择优先级"});
                return null;
            }
            console.log("prepareModel: "+JSON.stringify(sendData));
            return sendData;
        },

        sendAddRecordSignal: function () {
            var model = Record.Signal_Util.prepareSignalMode();
            var sendData = JSON.stringify(model);
            if(model != null) {
                Record.Signal_Dao.addRecordSignal({data:sendData}, function (data) {
                    if (data != null ) {
                        switch (data.code) {
                            case "A00000":
                                Record.Signal_Util.reloadHandler(data);
                                $("form[role=addForm]").dialog("close");
                                break;
                            case "EPG00501":
                                EpgWeb.Plugin.showTips({theme: "error", content: "创建失败：信号名称重复！"});
                                break;
                            default:
                                EpgWeb.Plugin.showTips({theme: "error", content: "接口返回异常："+data.code});
                                console.log("request : addRecordSignal data.code " + data.code);
                                break;
                        }

                    } else {
                        EpgWeb.Plugin.showTips({theme: "error", content: "创建失败"});
                    }

                })
            }else {
                console.log("sendAddRecordSignal sendData == null ?!");
            }

        },

        sendEditRecordSignal: function () {
            var model = Record.Signal_Util.prepareSignalMode();
            var sendData = JSON.stringify(model);
            console.log("---" + sendData);

            function editSignal() {
                Record.Signal_Dao.editRecordSignal({data: sendData}, function (data) {
                    if (data != null && data.code == "A00000") {
                        Record.Util.reloadHandler(data)
                    } else {
                        EpgWeb.Plugin.showTips({theme: "error", content: "修改失败"});
                    }
                    $("form[role=addForm]").dialog("close")

                });
            }
            if(model != null) {
                //修改时判断model是否设置下线，是的话查询关联信号，关联了提示确认窗口
                if (model.status === 0) {
                    var params = {paramsObj: JSON.stringify({'EQL_recordChannelSignal.signal_id': model.id})};
                    Record.Channel_Signal_Dao.getRecordChannelSignalList(params, function (data) {
                        if (data != null && data.code == "A00000") {
                            SystemDialog.queryConfirm("该信号已经被关联，确认下线吗？", function (flag) {
                                if (flag === true) {
                                    editSignal();
                                }
                            })

                        }else{
                            editSignal();
                        }

                    });
                }else{
                    editSignal();
                }

            }else{
                console.log("sendEditRecordSignal sendData == null ?!");
            }

        },


        delSignal: function (entityId) {
            SystemDialog.queryConfirm("确认删除？", function (flag) {
                if (flag === true) {
                    Record.Signal_Dao.deleteRecordSignal({id: entityId }, function (data) {
                        Record.Util.reloadHandler(data);
                    })
                }
            })
        },
        openSignalModifyDlg: function (entityId) {
            var parentDom = $("form[role=addForm]");
            if (entityId > 0) {
                Record.Signal_Dao.getRecordSignal(entityId, function (data) {
                    if (data != null) {
                        //显示修改按钮
                        var $sendAddButton = $("button[name=add_signal_btn]");
                        var $sendEditButton = $("button[name=edit_signal_btn]");
                        $sendAddButton.hide();
                        $sendEditButton.show();
                        //赋值
                        var resultData = data.data;
                        parentDom.find("input[name=signal_id]").val(resultData.id);
                        parentDom.find("input[name=signal_name]").val(resultData.signalName);
                        parentDom.find("input[name=signal_address_name]").val(resultData.signalAddress);
                        parentDom.find("input[name=server_cluster_name]").val(resultData.serverCluster);
                        parentDom.find("select[name=priority_name]").val(resultData.priority);
                        parentDom.find("select[name=status]").val(resultData.status);

                        console.log("modify data: " + JSON.stringify(data));
                        parentDom.dialog("option", "title", "修改信号").dialog("open");
                    }else {

                    }
                })
            } else {

                EpgWeb.Plugin.showTips({theme: "error", content: "id 异常"});
            }
        },

        sendAddRecordChannelSignal: function () {
            var model = Record.Signal_Util.prepareChannelSignalMode();
            var sendData = JSON.stringify(model);
            if(model != null) {
                Record.Channel_Signal_Dao.addRecordChannelSignal({data:sendData}, function (data) {
                    if (data != null ) {
                        switch (data.code) {
                            case "A00000":
                                Record.Signal_Util.reloadHandler(data);
                                $("form[role=addForm]").dialog("close");
                                break;
                            case "EPG00501":
                                EpgWeb.Plugin.showTips({theme: "error", content: "创建失败：信号名称重复！"});
                                break;
                            default:
                                EpgWeb.Plugin.showTips({theme: "error", content: "接口返回异常："+data.code});
                                console.log("request : addRecordSignal data.code " + data.code);
                                break;
                        }

                    } else {
                        EpgWeb.Plugin.showTips({theme: "error", content: "创建失败"});
                    }

                })
            }else {
                console.log("sendAddRecordChannelSignal sendData == null ?!");
            }

        },
        sendEditRecordChannelSignal: function () {
            var model = Record.Signal_Util.prepareChannelSignalMode();
            var sendData = JSON.stringify(model);
            console.log("---" + sendData);
            if(model != null) {
                Record.Channel_Signal_Dao.editRecordChannelSignal({data:sendData}, function (data) {
                    if (data != null && data.code == "A00000") {
                        Record.Util.reloadHandler(data)
                    } else {
                        EpgWeb.Plugin.showTips({theme: "error", content: "修改失败"});
                    }
                    $("form[role=addForm]").dialog("close")

                })
            }else{
                console.log("sendEditRecordSignal sendData == null ?!");
            }

        },

        prepareChannelSignalMode: function () {
            var sendData = {};
            var parentDom = $("form[role=addForm]");
            var channelId = $("#title_channel_id").text();
            sendData.signalId = parentDom.find("input[name=signal_name]").attr("data-id");
            sendData.signalName = parentDom.find("input[name=signal_name]").val().trim();
            sendData.channelId = channelId;
            if ($.trim(sendData.signalName).length === 0) {
                EpgWeb.Plugin.showTips({ theme: "error", content: "请填写信号名称"});
                return null;
            }
            if ($.trim(sendData.signalId).length === 0) {
                EpgWeb.Plugin.showTips({ theme: "error", content: "信号名称有误"});
                return null;
            }

            console.log("prepareModel: "+JSON.stringify(sendData));
            return sendData;
        },
        delChannelSignal: function (entityId) {
            console.log("wait to do");
            SystemDialog.queryConfirm("确认删除？", function (flag) {
                if (flag === true) {
                    Record.Channel_Signal_Dao.deleteRecordChannelSignal({id: entityId }, function (data) {
                        if (data != null ) {
                            switch (data.code) {
                                case "A00000":
                                    Record.Signal_Util.reloadHandler(data);
                                    break;
                                case "EPG00502":
                                    EpgWeb.Plugin.showTips({theme: "error", content: data.msg});
                                    break;
                                default:
                                    EpgWeb.Plugin.showTips({theme: "error", content: data.msg});
                                    console.log("request : delChannelSignal data.code " + data.code);
                                    break;
                            }

                        } else {
                            EpgWeb.Plugin.showTips({theme: "error", content: "删除失败"});
                        }

                    })
                }
            })
        },

        startChannelSignal: function (entityId) {
            console.log("wait to do");
            SystemDialog.queryConfirm("确认启动？", function (flag) {
                if (flag === true) {
                    Record.Channel_Signal_Dao.startRecordChannelSignal({id: entityId }, function (data) {
                        Record.Util.reloadHandler(data);
                    })
                }
            })
        },

        stopChannelSignal: function (entityId) {
            console.log("wait to do");
            SystemDialog.queryConfirm("确认停止？", function (flag) {
                if (flag === true) {
                    Record.Channel_Signal_Dao.stopRecordChannelSignal({id: entityId }, function (data) {
                        Record.Util.reloadHandler(data);
                    })
                }
            })
        },
        openChannelSignalModifyDlg: function (entityId) {
            var parentDom = $("form[role=addForm]");
            if (entityId > 0) {
                Record.Signal_Dao.getRecordChannelSignal(entityId, function (data) {
                    if (data != null) {
                        //显示修改按钮
                        var $sendAddButton = $("button[name=add_channel_signal_btn]");
                        var $sendEditButton = $("button[name=edit_channel_signal_btn]");
                        $sendAddButton.hide();
                        $sendEditButton.show();
                        //赋值
                        var resultData = data.data;
                        parentDom.find("input[name=signal_id]").val(resultData.id);
                        parentDom.find("input[name=signal_name]").val(resultData.signalName);
                        parentDom.find("input[name=signal_address_name]").val(resultData.signalAddress);
                        parentDom.find("input[name=server_cluster_name]").val(resultData.serverCluster);
                        parentDom.find("select[name=priority_name]").val(resultData.priority);
                        parentDom.find("select[name=status]").val(resultData.status);

                        console.log("modify data: " + JSON.stringify(data));
                        parentDom.dialog("option", "title", "修改信号").dialog("open");
                    }else {

                    }
                })
            } else {

                EpgWeb.Plugin.showTips({theme: "error", content: "id 异常"});
            }
        }

    });
});